package com.py.code.controller;

import java.text.SimpleDateFormat;
import java.util.*;
import java.io.IOException;
import javax.management.Query;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.druid.sql.ast.statement.SQLForeignKeyImpl;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.py.code.domain.TbCodingrules;
import com.py.code.service.ITbCodingrulesService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 编码规则Controller
 *
 * @author 童佳
 * @date 2024-11-13
 */
@RestController("/tbcodingrules")
public class TbCodingrulesController extends BaseController {
    @Autowired
    private ITbCodingrulesService tbCodingrulesService;


    /**
     * 查询编码规则列表
     */
    @RequiresPermissions("code:tbcodingrules:list")
    @GetMapping("/list")
    public TableDataInfo list(TbCodingrules tbCodingrules) {
        startPage();
        List<TbCodingrules> list = tbCodingrulesService.selectTbCodingrulesList(tbCodingrules);
        return getDataTable(list);
    }

    /**
     * 导出编码规则列表
     */
    @RequiresPermissions("code:tbcodingrules:export")
    @Log(title = "编码规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbCodingrules tbCodingrules) {
        List<TbCodingrules> list = tbCodingrulesService.selectTbCodingrulesList(tbCodingrules);
        ExcelUtil<TbCodingrules> util = new ExcelUtil<TbCodingrules>(TbCodingrules.class);
        util.exportExcel(response, list, "编码规则数据");
    }

    /**
     * 获取编码规则详细信息
     */
    @RequiresPermissions("code:tbcodingrules:query")
    @GetMapping(value = "/{enCode}")
    public AjaxResult getInfo(@PathVariable("enCode") Integer enCode) {
        return success(tbCodingrulesService.selectTbCodingrulesByEnCode(enCode));
    }

    /**
     * 新增编码规则
     */
    @RequiresPermissions("code:tbcodingrules:add")
    @Log(title = "编码规则", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbCodingrules tbCodingrules) {
        tbCodingrules.setCreateBy(SecurityUtils.getUsername());
        tbCodingrules.setCreateTime(new Date());
        System.out.println(tbCodingrules);
        return toAjax(tbCodingrulesService.insertTbCodingrules(tbCodingrules));
    }

    /**
     * 修改编码规则
     */
    @RequiresPermissions("code:tbcodingrules:edit")
    @Log(title = "编码规则", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbCodingrules tbCodingrules) {
        return toAjax(tbCodingrulesService.updateTbCodingrules(tbCodingrules));
    }

    /**
     * 删除编码规则
     */
    @RequiresPermissions("code:tbcodingrules:remove")
    @Log(title = "编码规则", businessType = BusinessType.DELETE)
    @DeleteMapping("/{enCodes}")
    public AjaxResult remove(@PathVariable Integer[] enCodes) {
        return toAjax(tbCodingrulesService.deleteTbCodingrulesByEnCodes(enCodes));
    }

    public String getLastDate(int id) {
        String time = tbCodingrulesService.selectByLast_dateString(id);
        return time;
    }

    /**
     * 自动生成编码
     */
    @RequestMapping("/generateCode")
    public synchronized Map<String, Object> generateCode(@RequestParam(value = "code", required = true) Integer code) throws IOException {
        Date now = new Date();
        String currentDateMonthAndDay = new SimpleDateFormat("yyyy-MM-dd").format(now);
        if (!currentDateMonthAndDay.equals(getLastDate(code))) {
            System.out.println("进来了");
            TbCodingrules tbCodingrules = new TbCodingrules();
            tbCodingrules.setEnCode(code);
            tbCodingrulesService.updateMax_numInt(tbCodingrules);
        }

        TbCodingrules t = tbCodingrulesService.selectTbCodingrulesByEnCode(code);
        String currentDate = null;
        int number = 1;
        if (t.getEnTime().equals("年月日时分秒")) {
            currentDate = new SimpleDateFormat("yyyyMMddHHmmss").format(now);
        } else if (t.getEnTime().equals("年月日时分")) {
            currentDate = new SimpleDateFormat("yyyyMMddHHmm").format(now);
        } else if (t.getEnTime().equals("年月日")) {
            currentDate = new SimpleDateFormat("yyyyMMdd").format(now);
        }
        Integer max = t.getMaxNum();
        int result;
        if (max == null) {
            result = 0;
        } else {
            result = t.getMaxNum();
        }
        number = result + t.getEnStep();
        Map<String,Object> map=new HashMap<>();
        String sequence = t.getEnPrefix() + String.format("%s%0" + Integer.toString(t.getEnNum()) + "d", currentDate, number);
        map.put("sequence", sequence);
        map.put("number",number);
        map.put("codeId", code);
        return map;
    }

    // 新增一个方法用于保存
        @RequestMapping("/saveCode")
        public synchronized void saveCode(@RequestParam(value = "code", required = true) Integer code, @RequestParam(value = "number", required = true) Integer number) throws IOException {
            TbCodingrules tbCodingrules = new TbCodingrules();
            tbCodingrules.setEnCode(code);
            tbCodingrules.setMaxNum(number);
            tbCodingrules.setLastDate(new Date());
            tbCodingrulesService.update(tbCodingrules);
        }


}
